﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Utils;
using StackExchange.Profiling;
using WebUI.Infrastructure;

namespace WebUI
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //home index
            routes.MapRouteLowercase(null,
                "",
                new { controller = "Home", action = "Index" }
            );

            //error
            routes.MapRouteLowercase(null,
                "404",
                new { controller = "Error", action = "Error404" }
            );
            //site
            routes.MapRouteLowercase(null,
                "contact",
                new { controller = "Site", action = "Contact" }
            );

            routes.MapRouteLowercase(null,
                "jobs",
                new { controller = "Search", action = "Jobs" }
            );
            routes.MapRouteLowercase(null,
                "jobs/tag/{tag}",
                new { controller = "Search", action = "Jobs" }
            );
            routes.MapRouteLowercase(null,
                "shots",
                new { controller = "Search", action = "Shots" }
            );
            routes.MapRouteLowercase(null,
                "designers",
                new { controller = "Search", action = "Designers" }
            );

            routes.MapRouteLowercase(null,
                "signin", 
                new { controller = "Account", action = "SignIn" } 
            );
            routes.MapRouteLowercase(null,
                "signout",
                new { controller = "Account", action = "SignOut" }
            );
            routes.MapRouteLowercase(null,
                "signup",
                new { controller = "Account", action = "SignUp" }
            );
            routes.MapRouteLowercase(null,
                "forgotpassword",
                new { controller = "Account", action = "ForgotPassword" }
            );
            routes.MapRouteLowercase(null,
                "resetpassword",
                new { controller = "Account", action = "ResetPassword" }
            );
            routes.MapRouteLowercase(null,
                "account",
                new { controller = "Account", action = "EditAccountProfile" }
            );

            

            routes.MapRouteLowercase(null,
                "admin",
                new { controller = "Admin", action = "Index" }
            );


            routes.MapRouteLowercase(null,
                "job/about-posting",
                new { controller = "Co", action = "AboutPosting" }
            );
            routes.MapRouteLowercase(null,
                "job/post",
                new { controller = "Co", action = "JobPosting" }
            );
            routes.MapRouteLowercase(null,
                "job/preview",
                new { controller = "Co", action = "JobPostingPreview" }
            );
            routes.MapRouteLowercase(null,
                "job/pay",
                new { controller = "Co", action = "GoPayment" }
            );
            routes.MapRouteLowercase(null,
                "job/buycredits",
                new { controller = "Co", action = "BuyCredits" }
            );
            routes.MapRouteLowercase(null,
                "job/edit",
                new { controller = "Co", action = "EditJob" }
            );
            routes.MapRouteLowercase(null,
                "jobs/{jobid}/{jobtitle}",
                new { controller = "Co", action = "Job" }
            );

            routes.MapRouteLowercase(null,
                "company/detail/{coID}",
                new { controller = "Co", action = "Detail" }
            );
            routes.MapRouteLowercase(null,
                "company/editprofile",
                new { controller = "Co", action = "EditProfile" }
            );
            routes.MapRouteLowercase(null,
                "company/editpassword",
                new { controller = "Co", action = "EditPassword" }
            );
            routes.MapRouteLowercase(null,
                "company/joblist",
                new { controller = "Co", action = "JobList" }
            );
            routes.MapRouteLowercase(null,
                "company/billinghistory",
                new { controller = "Co", action = "BillingHistory" }
            );
            routes.MapRouteLowercase(null,
                "company/savedtalent",
                new { controller = "Co", action = "SavedTalent" }
            );
            routes.MapRouteLowercase(null,
                "company/savedjobs",
                new { controller = "Co", action = "SavedJobs" }
            );
            /***********BEGIN: has variable**********************/

            routes.MapRouteLowercase(null,
                "user/{userName}",
                new { controller = "Ind", action = "Shots"}
            );
            routes.MapRouteLowercase(null,
                "user/{userName}/projects",
                new { controller = "Ind", action = "Projects"}
            );
            routes.MapRouteLowercase(null,
                "user/{userName}/tags",
                new { controller = "Ind", action = "Tags"}
            );
            routes.MapRouteLowercase(null,
                "user/{userName}/baskets",
                new { controller = "Ind", action = "Baskets"}
            );
            routes.MapRouteLowercase(null,
                "user/{userName}/likes",
                new { controller = "Ind", action = "Likes"}
            );
            routes.MapRouteLowercase(null,
                "user/{userName}/resume",
                new { controller = "Ind", action = "Resume"}
            );
            routes.MapRouteLowercase(null,
                "ind/savedtalent",
                new { controller = "Ind", action = "SavedTalent" }
            );
            routes.MapRouteLowercase(null,
                "ind/savedjobs",
                new { controller = "Ind", action = "SavedJobs" }
            );

            routes.MapRouteLowercase(null,
                "shots/{ShotID}",
                new { controller = "Ind", action = "Shot" }
            );
            routes.MapRouteLowercase(null,
                "shots/{ShotID}/original",
                new { controller = "Ind", action = "ShotOriginal" }
            );
            routes.MapRouteLowercase(null,
                "shots/{ShotID}/addtag",
                new { controller = "Ind", action = "AddShotTag" }
            );
            routes.MapRouteLowercase(null,
                "shots/{ShotID}/edit",
                new { controller = "Ind", action = "EditShot" }
            );
            routes.MapRouteLowercase(null,
                "shots/{ShotID}/delete",
                new { controller = "Ind", action = "DeleteShot" }
            );

            routes.MapRouteLowercase(
                null, // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            // View engine
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new DerivedRazorViewEngine());

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            GlobalUtilities.GetInstance().Init();
        }

        protected void Application_BeginRequest()
        {
            MiniProfiler profiler = null;

            // might want to decide here (or maybe inside the action) whether you want
            // to profile this request - for example, using an "IsSystemAdmin" flag against
            // the user, or similar; this could also all be done in action filters, but this
            // is simple and practical; just return null for most users. For our test, we'll
            // profile only for local requests (seems reasonable)
            if (GlobalUtilities.GetInstance().NeedTiming)
            {
                profiler = StackExchange.Profiling.MiniProfiler.Start();
            }
        }

        protected void Application_EndRequest()
        {
            if (GlobalUtilities.GetInstance().NeedTiming)
            {
                StackExchange.Profiling.MiniProfiler.Stop();
            }
        }

        protected void Application_Error() {
            //Response.Clear();
            //Server.ClearError();
            //Response.Redirect("/404");
        }
    }
}